<template>
  <div class="recourse-table">
    <el-dialog
      title="编辑资源"
      :visible.sync="isEdite"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="资源名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="资源路径">
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="资源分类">
          <el-select v-model="form.categoryId" placeholder="请选择上级菜单">
            <el-option label="无上级菜单" :value="-1"></el-option>
            <el-option
              :label="item.name"
              :value="item.id"
              :key="item.id"
              v-for="item in parentMenuList"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('isshow', false)">取 消</el-button>
        <el-button type="primary" @click="updateResource">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRourse,
  getResourceCategories,
  updateDate
} from '@/services/resource'

export default {
  name: 'resourceTable',
  props: {
    isEdite: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [String, Number],
      require: true
    }
  },
  data () {
    return {
      form: {
        id: this.courseId,
        name: '',
        url: '',
        description: '',
        categoryId: null
      },
      parentMenuList: []
    }
  },
  created () {
    this.getrourseList()
    this.getscourseID()
  },
  methods: {
    async getrourseList () {
      const { data } = await getRourse(this.courseId)
      console.log(data)
      if (data.code === '000000') {
        this.form = data.data
      }
    },
    handleClose (done) {
      this.$emit('isshow', false)
    },
    async getscourseID () {
      const { data } = await getResourceCategories()
      this.parentMenuList = data.data
    },
    async updateResource () {
      console.log(this.form)
      const { data } = await updateDate(this.form)
      console.log(data)
      if (data.code === '000000') {
        this.$message.success('更新成功')
      } else {
        this.$message.error(data.mesg)
      }
      this.$emit('isshow', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
